package com.student91.recruit.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.CellType;

import java.io.*;
import java.util.Calendar;

/**
 * 生成导出Excel文件对象
 */
public class XLSExportUtil {
    // 设置cell编码解决中文高位字节截断
    // 定制日期格式
    private static String DATE_FORMAT = " m/d/yy "; // "m/d/yy h:mm"
    // 定制浮点数格式
    private static String NUMBER_FORMAT = " #,##0.00 ";
    private String xlsFileName;
    private String path;
    private HSSFWorkbook workbook;

    public HSSFWorkbook getWorkbook() {
        return workbook;
    }

    public void setWorkbook(HSSFWorkbook workbook) {
        this.workbook = workbook;
    }

    private HSSFSheet sheet;
    private HSSFRow row;

    /**
     * 初始化Excel
     *
     * @param fileName 导出文件名
     */
    public XLSExportUtil(String fileName, String path) {
        this.xlsFileName = fileName;
        this.path = path;
        this.workbook = new HSSFWorkbook();
        this.sheet = workbook.createSheet();
    }

    //导出Excel文件
    public void exportXLS() throws Exception {
        try {
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            FileOutputStream fOut = new FileOutputStream(path + File.separator + xlsFileName);
            workbook.write(fOut);
            fOut.flush();
            fOut.close();
        } catch (FileNotFoundException e) {
            throw new Exception(" 生成导出Excel文件出错! ", e);
        } catch (IOException e) {
            throw new Exception(" 写入Excel文件出错! ", e);
        }
    }

    //导出Excel文件
    public void exportXLS(OutputStream outputStream) throws Exception {
        workbook.write(outputStream);
        workbook.close();
    }

    /**
     * 增加一行
     *
     * @param index 行号
     */
    public void createRow(int index) {
        this.row = this.sheet.createRow(index);
    }

    /**
     * 设置单元格
     *
     * @param index 列号
     * @param value 单元格填充值
     */
    @SuppressWarnings("deprecation")
    public void setCell(int index, String value) {
        HSSFCell cell = this.row.createCell((short) index);
        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(value);
    }

    /**
     * 设置单元格
     *
     * @param index 列号
     * @param value 单元格填充值
     */
    @SuppressWarnings("deprecation")
    public void setCell(int index, Calendar value) {
        HSSFCell cell = this.row.createCell((short) index);
        cell.setCellValue(value.getTime());
        HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
        cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 设置cell样式为定制的日期格式
        cell.setCellStyle(cellStyle); // 设置该cell日期的显示格式
    }

    /**
     * 设置单元格
     *
     * @param index 列号
     * @param value 单元格填充值
     */
    public void setCell(int index, int value) {
        HSSFCell cell = this.row.createCell((short) index);
        cell.setCellType(CellType.NUMERIC);
        cell.setCellValue(value);
    }

    /**
     * 设置单元格
     *
     * @param index 列号
     * @param value 单元格填充值
     */
    public void setCell(int index, double value) {
        HSSFCell cell = this.row.createCell((short) index);
        cell.setCellType(CellType.NUMERIC);
        cell.setCellValue(value);
        HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
        HSSFDataFormat format = workbook.createDataFormat();
        cellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); // 设置cell样式为定制的浮点数格式
        cell.setCellStyle(cellStyle); // 设置该cell浮点数的显示格式
    }

}
